package com.zyfdroid.bigdialerkit.services;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import com.zyfdroid.bigdialerkit.receivers.CallingChangedReceiver;

public class CallNotficationListenerService extends NotificationListenerService {
    private static final String TAG = "CallNotification";

    // 电话应用包名（可能因设备而异）
    private static final String[] PHONE_PACKAGES = {
            "com.sonymobile.android.dialer",      // 原生拨号应用
            "com.android.dialer",      // 原生拨号应用
            "org.fossify.phone",      // 原生拨号应用
            "com.android.phone",      // 原生拨号应用
            "com.google.android.dialer", // Google拨号应用
            "com.samsung.android.dialer" // 三星拨号应用
    };

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        String packageName = sbn.getPackageName();
        Log.d(TAG, "收到通知: " + packageName);

        // 检查是否是电话应用的通知
        if (isPhoneNotification(packageName)) {
            Log.d(TAG, "检测到电话通知，刷新小部件");
            refreshWidget();
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        String packageName = sbn.getPackageName();
        if (isPhoneNotification(packageName)) {
            Log.d(TAG, "电话通知被移除，刷新小部件");
            refreshWidget();
        }
    }

    private boolean isPhoneNotification(String packageName) {
        for (String phonePkg : PHONE_PACKAGES) {
            if (phonePkg.equals(packageName)) {
                return true;
            }
        }
        return false;
    }

    private void refreshWidget() {
        CallingChangedReceiver.notifyUpdate(this);
    }
}